﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace Zove.Desktop.DragAndRun {
    public partial class ConfigurationView : Form {

        public Font SelectedFont {
            get;
            set;
        }

        public ConfigurationView() {
            InitializeComponent();
        }

        private void btnFont_Click(object sender, EventArgs e) {
            FontDialog f = new FontDialog();
            f.Font = SelectedFont;

            if (f.ShowDialog() == DialogResult.OK) {
                SelectedFont = f.Font;
            }
        }

        private void btnOk_Click(object sender, EventArgs e) {
            if (!File.Exists(this.tbDefaultSourceEditor.Text)) {
                MessageBox.Show(
                    "The program file is not existed, please choose again.");
                return;
            }

            ConfigurationManager.AppSettings["font type"] = SelectedFont.Name;
            ConfigurationManager.AppSettings["font size"] = SelectedFont.Size.ToString();
            ConfigurationManager.AppSettings["editor"] = this.tbDefaultSourceEditor.Text;
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
        }

        private void ConfigurationView_Load(object sender, EventArgs e) {
            string fontType = ConfigurationManager.AppSettings["font type"];
            double size = Convert.ToDouble(
                ConfigurationManager.AppSettings["font size"]);
            Font font = new Font(fontType, (float)size);
            this.SelectedFont = font;

            this.tbDefaultSourceEditor.Text = ConfigurationManager.AppSettings["editor"];
        }

        private void btnOpenEditor_Click(object sender, EventArgs e) {
            OpenFileDialog d = new OpenFileDialog();

            d.Filter = "*.exe | *.exe";
            if (d.ShowDialog() == DialogResult.OK) {
                this.tbDefaultSourceEditor.Text = d.FileName;
            }
        }

        private void btnAddModelLibrary_Click(object sender, EventArgs e) {
            FolderBrowserDialog d = new FolderBrowserDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                this.lbModelLibraries.Items.Add(d.SelectedPath);
            }
        }

        private void btnOpenModelLibrary_Click(object sender, EventArgs e) {
            Process x = new Process();

            if (this.lbModelLibraries.SelectedValue != null) {
                x.StartInfo.FileName = this.lbModelLibraries.SelectedValue.ToString();
                x.Start();
            }
        }

        private void btnRemoveModelLibrary_Click(object sender, EventArgs e) {
            this.lbModelLibraries.Items.Remove(this.lbModelLibraries.SelectedItem);
        }

        private void AddScriptLibrary_Click(object sender, EventArgs e) {
            FolderBrowserDialog d = new FolderBrowserDialog();

            if (d.ShowDialog() == DialogResult.OK) {
                this.lbScriptLibraries.Items.Add(d.SelectedPath);
            }
        }

        private void btnOpenScriptLibrary_Click(object sender, EventArgs e) {
            Process x = new Process();

            if (this.lbScriptLibraries.SelectedValue != null) {
                x.StartInfo.FileName = this.lbScriptLibraries.SelectedValue.ToString();
                x.Start();
            }
        }

        private void btnRemoveScriptLibrary_Click(object sender, EventArgs e) {
            this.lbScriptLibraries.Items.Remove(this.lbScriptLibraries.SelectedItem);
        }
    }
}
